package com.xoms.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xoms.admin.controller.res.BaseResult;
import com.xoms.admin.controller.res.ListResult;
import com.xoms.admin.controller.res.ObjectResult;
import com.xoms.admin.controller.res.bean.RProStockBean;
import com.xoms.admin.controller.res.bean.RProStockInBean;
import com.xoms.admin.controller.res.bean.RProStockInInfoBean;
import com.xoms.admin.controller.res.bean.RProStoreBean;
import com.xoms.admin.controller.res.bean.RProviderBean;
import com.xoms.admin.controller.res.bean.RPurchaseBean;
import com.xoms.admin.controller.res.bean.RPurchaseInfoBean;
import com.xoms.admin.controller.springmvc.req.DelProStoreParam;
import com.xoms.admin.controller.springmvc.req.DelProviderParam;
import com.xoms.admin.controller.springmvc.req.DelPurchaseParam;
import com.xoms.admin.controller.springmvc.req.DelStockInParam;
import com.xoms.admin.controller.springmvc.req.EditProStoreParam;
import com.xoms.admin.controller.springmvc.req.EditProviderParam;
import com.xoms.admin.controller.springmvc.req.EditPurchaseParam;
import com.xoms.admin.controller.springmvc.req.EditStockInParam;
import com.xoms.admin.controller.springmvc.req.GetProStocksParam;
import com.xoms.admin.controller.springmvc.req.GetProStoresParam;
import com.xoms.admin.controller.springmvc.req.GetProviderParam;
import com.xoms.admin.controller.springmvc.req.GetPurchaseDetailsParam;
import com.xoms.admin.controller.springmvc.req.GetPurchasesParam;
import com.xoms.admin.controller.springmvc.req.GetStockInDetailsParam;
import com.xoms.admin.controller.springmvc.req.GetStockInsParam;
import com.xoms.admin.controller.springmvc.req.Purchase4StockInParam;
import com.xoms.admin.controller.springmvc.req.SubPurchaseParam;
import com.xoms.admin.controller.springmvc.req.SubStockInParam;
import com.xoms.admin.model.AccountBean;
import com.xoms.admin.service.ProStockInService;
import com.xoms.admin.service.ProStockService;
import com.xoms.admin.service.ProviderService;
import com.xoms.admin.service.PurchaseService;
import com.xoms.admin.service.exception.ServiceException;
import com.xoms.admin.service.res.PageResult;
import com.xoms.util.NumUtil;

@Component
public class ProStockController {

	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	ProStockService proStockServiceImpl;

	@Autowired
	ProviderService providerServiceImpl;

	@Autowired
	ProStockInService proStockInServiceImpl;

	@Autowired
	PurchaseService purchaseServiceImpl;

	/**
	 * 获取柜台仓库列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProStoreBean> getStores(GetProStoresParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProStoreBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String key = param.getKey();// 关键字
			int type = NumUtil.toInt(param.getType(), 0);// 默认全部显示
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取商品分类列表
			PageResult<RProStoreBean> resData = proStockServiceImpl.getStoreHouses(account, key, page, pageSize, type);

			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 添加/修改柜台仓库信息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProStoreBean> editStore(EditProStoreParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProStoreBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String sid = param.getSid();
			String name = param.getName();
			String theatreId = param.getTheatreId();
			String remark = param.getRemark();
			Double limitMoney = param.getLimitMoney();
			Double totalCost = param.getTotalCost();
			String adminId = param.getAdminId();
			int type = NumUtil.toInt(param.getType(), 1);// 默认仓库
			int status = NumUtil.toInt(param.getIsEnable(), 1);// 默认启用
			RProStoreBean data = proStockServiceImpl.editStoreHouse(account, sid, name, theatreId, remark, limitMoney, type,
					status, totalCost, adminId);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除柜台仓库
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delStore(DelProStoreParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String sid = param.getSid();
			boolean flag = proStockServiceImpl.del(account, sid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 商品库存列表，根据仓库获取商品列表及数量
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProStockBean> getProStocks(GetProStocksParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProStockBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int pageSize = NumUtil.toInt(param.getPageSize(), 20);
			String storeHouseId = param.getStoreHouseId();
			// 获取商品分类列表
			PageResult<RProStockBean> resData = proStockServiceImpl.getProStocks(account, key, page, pageSize,
					storeHouseId);

			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取供应商列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProviderBean> getProvider(GetProviderParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProviderBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			// 获取商品分类列表
			PageResult<RProviderBean> resData = providerServiceImpl.getProvider(account, key, page, PageSize);

			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除供应商
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delProvider(DelProviderParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int pid = NumUtil.toInt(param.getPid(), -1);
			boolean flag = providerServiceImpl.del(account, pid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 添加/修改供应商信息
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProviderBean> editProvider(EditProviderParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProviderBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			int pid = NumUtil.toInt(param.getPid(), 0);
			String name = param.getName();
			String theatreId = param.getTheatreId();
			String linkMan = param.getLinkMan();
			String phoneNum = param.getPhoneNum();
			String addr = param.getAddr();
			String remark = param.getRemark();
			int status = NumUtil.toInt(param.getStatus(), 1);
			RProviderBean data = providerServiceImpl.edit(account, pid, theatreId, name, linkMan, phoneNum, addr, remark, status);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取商品采购列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RPurchaseBean> getPurchases(GetPurchasesParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RPurchaseBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String startTime = param.getStartTime();// 起始时间
			String endTime = param.getStartTime();// 终止时间
			String storeId = param.getStoreHouseId();// 仓库id

			// 获取采购单列表
			PageResult<RPurchaseBean> resData = purchaseServiceImpl.getPurchases(account, key, page, PageSize,
					startTime, endTime, storeId);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取采购单详情列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RPurchaseInfoBean> getPurchaseDetails(GetPurchaseDetailsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RPurchaseInfoBean> result = new ListResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");

			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			int paging = NumUtil.toInt(param.getPaging(), 0);// 默认不分页
			String purchaseId = param.getPurchaseId();
			// 获取采购详情列表
			PageResult<RPurchaseInfoBean> resData = purchaseServiceImpl.getPurchaseDetails(account, key, page, PageSize,
					purchaseId, paging);
			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 编辑/新建采购单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RPurchaseBean> editPurchase(EditPurchaseParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RPurchaseBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			String pid = param.getPid();// 为空则新建
			String storeId = param.getStoreHouseId();// 仓库id
			String batch = param.getBatch();
			Double totalCost = NumUtil.toDouble(param.getTotalCost(), 0);
			String remark = param.getRemark();// 描述
			String details = param.getDetails();// 采购详情
			String providerId = param.getProviderId();// 供应商id
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			// 成功后返回编辑后商品采购单信息
			RPurchaseBean data = purchaseServiceImpl.edit(account, pid, storeId, batch, remark, totalCost, details,
					providerId, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除采购单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delPurchase(DelPurchaseParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String purchaseId = param.getPid();// 商品采购单id
			boolean flag = purchaseServiceImpl.del(account, purchaseId);// 删除采购单信息
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 采购单提交审核
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult submitPurchase(SubPurchaseParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String purchaseId = param.getPid();// 商品采购单id
			boolean flag = purchaseServiceImpl.submitPurchase(account, purchaseId);// 提交审核
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取入库单列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProStockInBean> getStockIns(GetStockInsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProStockInBean> result = new ListResult<>();
		try {

			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String endTime = param.getEndTime();// 结束搜索时间
			String startTime = param.getStartTime();// 开始搜索时间
			String storeHouseId = param.getStoreHouseId();// 仓库id
			// 获取商品入库单列表
			PageResult<RProStockInBean> resData = proStockInServiceImpl.getProstockIn(account, key, page, PageSize,
					startTime, endTime, storeHouseId);

			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 获取入库详情列表
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ListResult<RProStockInInfoBean> getStockInDetails(GetStockInDetailsParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ListResult<RProStockInInfoBean> result = new ListResult<>();
		try {

			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String key = param.getKey();// 关键字
			int page = NumUtil.toInt(param.getPage(), 0);
			int PageSize = NumUtil.toInt(param.getPageSize(), 20);
			String StockInid = param.getStockInId();// 入库单id
			int paging = NumUtil.toInt(param.getPaging(), 0);// 默认不分页
			// 获取商品入库详情列表
			PageResult<RProStockInInfoBean> resData = proStockInServiceImpl.getProstockInDetails(account, key, page,
					PageSize, StockInid, paging);

			if (resData != null) {
				result.setData(resData.getData());
				result.setTotalSize(resData.getCount());
			}
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 采购入库
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProStockInBean> insertStockIn(Purchase4StockInParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProStockInBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号

			String storeId = param.getStoreHouseId();// 仓库id
			Double totalCost = NumUtil.toDouble(param.getTotalCost(), 0);
			String remark = param.getRemark();// 描述
			String details = param.getDetails();// 采购详情
			int type = NumUtil.toInt(param.getType(), 0);
			String targetId = param.getTargetId();
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			// 成功后返回编辑后商品入库信息
			RProStockInBean data = proStockInServiceImpl.insert(account, targetId, storeId, remark, totalCost,
					details, type, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 编辑入库单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public ObjectResult<RProStockInBean> editStockIn(EditStockInParam param, HttpServletRequest request,
			HttpServletResponse response) {
		ObjectResult<RProStockInBean> result = new ObjectResult<>();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String sid = param.getSid();
			String storeHouseId = param.getStoreHouseId();
			Double totalCost = NumUtil.toDouble(param.getTotalCost(), 0);
			Integer type = param.getType();
			String remark = param.getRemark();
			String details = param.getDetails();
			int isSubmit = NumUtil.toInt(param.getIsSubmit(), 1);// 是否直接提交1保存2直接提交

			RProStockInBean data = proStockInServiceImpl.edit(account, sid, storeHouseId, type, remark,
					totalCost, details, isSubmit);
			result.setData(data);
			result.setFlag(data != null);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 删除入库单
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult delStockIn(DelStockInParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String sid = param.getSid();
			boolean flag = proStockInServiceImpl.del(account, sid);
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

	/**
	 * 提交入库审核
	 * 
	 * @param param
	 * @param request
	 * @param response
	 * @return
	 */
	public BaseResult submitStockIn(SubStockInParam param, HttpServletRequest request, HttpServletResponse response) {
		BaseResult result = new BaseResult();
		try {
			AccountBean account = (AccountBean) request.getAttribute("account");// 登录账号
			String stockInId = param.getSid();// 入库单id
			boolean flag = proStockInServiceImpl.submit(account, stockInId);// 提交审核
			result.setFlag(flag);
		} catch (ServiceException e) {
			result.setServiceException(e);
		}
		return result;
	}

}
